package com.lyj.linearalgebra.admin.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.management.RuntimeErrorException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.lyj.linearalgebra.admin.domain.Admin;
import com.lyj.linearalgebra.post.domain.Posts;
import com.lyj.linearalgebra.user.domain.User;

import cn.itcast.jdbc.TxQueryRunner;

public class AdminDao {

	private QueryRunner qr = new TxQueryRunner();
	
	public Admin findByUsername(String username) {
		try {
			String sql = "select * from admin where username=?";
			return qr.query(sql, new BeanHandler<Admin>(Admin.class),username);
		} catch (SQLException e) {
            throw new RuntimeException(e);
		}
	}
	
	
	public Admin findByEmail(String email) {
		try {
			String sql = "select * from admin where email=?";
			return qr.query(sql, new BeanHandler<Admin>(Admin.class),email);
		} catch (SQLException e) {
            throw new RuntimeException(e);
		}
	}


	public void reset(String aid,String password) {
		String sql = "update admin set password=? where aid=?";
		try {
			qr.update(sql, password,aid);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}


	public void resetInfo(Admin admin, String username, String email) {

		String aid = admin.getAid();
		String sql;
		if(username != null && !username.trim().isEmpty()) {
			try {
				sql = "update admin set username=? where aid=?";
				qr.update(sql,username,aid);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		if(email != null && !email.trim().isEmpty()){
			try {
				sql = "update admin set email=? where aid=?";
				qr.update(sql,email,aid);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}


	public List<User> findAllUsers() {
		String sql = "select * from user";
		try {
			return qr.query(sql, new BeanListHandler<User>(User.class));
		} catch (SQLException e) {
            throw new RuntimeException(e);
		}
	}


	public void deleteUser(String uid) {
		String sql = "delete from user where uid=?";
		try {
			qr.update(sql,uid);
		} catch (SQLException e) {
            throw new RuntimeException(e);
		}
	}


	public List<Posts> getAllPosts() {
		String sql = "select * from posts";
		try {
			return qr.query(sql, new BeanListHandler<Posts>(Posts.class));
		} catch (SQLException e) {
            throw new RuntimeException(e);
		}
	}


	public void deletePost(String pid) {
		String sql = "delete from post where pid=?";
		try {
			qr.update(sql,pid);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}


	public void addManager(Admin form) {
		String sql = "insert into admin values(?,?,?,?)";
		Object[] params = {form.getAid(),form.getUsername(),form.getPassword(),form.getEmail()};
		try {
			qr.update(sql, params);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}
}
